//
//  HATipsPopView.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HATipsPopView.h"

@interface HATipsPopView ()

/***/
@property (nonatomic, strong) NSString *titleText;
@property (nonatomic, strong) NSString *contentText;
@property (nonatomic, strong) NSString *leftText;
@property (nonatomic, strong) NSString *rightText;

/***/
@property (nonatomic, strong) UILabel *titLabel;
/***/
@property (nonatomic, strong) UILabel *conLabel;
/***/
@property (nonatomic, strong) UIButton *leftButton;
/***/
@property (nonatomic, strong) UIButton *rightButton;
/***/
@property (nonatomic, strong) UIView *line1View;
/***/
@property (nonatomic, strong) UIView *line2View;
/***/
@property (nonatomic, strong) UIButton *closeButton;




@end

@implementation HATipsPopView


//title标题提示  right.2个按钮
-(instancetype)initWithTitle:(NSString *)title content:(NSString *)content left:(NSString *)left right:(NSString *)right {
    if (self = [super init]) {
        
        self.contentText = content;
        self.leftText = left;
        self.rightText = right;
        self.titleText = title;
        
        [self layoutViews];
    }
    return self;

}

-(void)buttomleftButtonClick:(UIButton *)button {
    [self dismiss];
    if (self.XZYGTiShiLeftBlock) {
        self.XZYGTiShiLeftBlock();
    }
}

-(void)buttomRightButtonClick:(UIButton *)button {
    [self dismiss];
        if (self.XZYGTiShiRightBlock) {
        self.XZYGTiShiRightBlock();
    }
}

-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    CGFloat alertViewW = factor_w(280);
    CGFloat alertViewH = factor_w(120);

    //计算文字内容高度
    NSDictionary *attrs = @{NSFontAttributeName :[UIFont systemFontOfSize:14]};
    CGSize maxSize = CGSizeMake(alertViewW-24, MAXFLOAT);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    CGFloat other = 42;
    
    if ([NSString isEmptyString:self.titleText]) {
        other += 28;
    }else {
        other += 50;
    }
    
    // 计算文字占据的宽高
    CGSize size = [self.contentText boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    if (size.height+10+other  < factor_w(120)) {
        alertViewH = factor_w(120);
    }else {
        alertViewH = size.height+10+other;
    }
    
    CGFloat alertViewX = (kUIScreen_w-alertViewW)*0.5;
    CGFloat alertViewY = (kUIScreen_h-alertViewH)*0.5;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.layer.cornerRadius = 5;
    self.alertView.layer.masksToBounds = YES;

    [self.alertView addSubview:self.line1View];
    [self.line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(0.5);
        make.bottom.mas_equalTo(-42);
    }];
    
    [self.alertView addSubview:self.line2View];
    [self.line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.centerX.mas_equalTo(self.alertView);
        make.top.mas_equalTo(self.line1View);
        make.bottom.mas_equalTo(0);
    }];
    
    if ([NSString isEmptyString:self.rightText]) {
        
        self.line2View.hidden = YES;
        
        [self.alertView addSubview:self.leftButton];
        [self.leftButton setTitle:self.leftText forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor colorWithHexString:@"#303133"] forState:0];
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(self.line1View.mas_bottom);
        }];
        
    }else {
        
        [self.alertView addSubview:self.leftButton];
        [self.leftButton setTitle:self.leftText forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor colorWithHexString:@"#303133"] forState:0];
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.mas_equalTo(0);
            make.top.mas_equalTo(self.line1View.mas_bottom);
            make.right.mas_equalTo(self.line2View.mas_left);
        }];

        [self.alertView addSubview:self.rightButton];
        [self.rightButton setTitle:self.rightText forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor colorWithHexString:@"#303133"] forState:0];
        [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(self.line1View.mas_bottom);
            make.left.mas_equalTo(self.line2View.mas_right);
        }];

    }
    
    if ([NSString isEmptyString:self.titleText]) {
        
        [self.alertView addSubview:self.conLabel];
        self.conLabel.text = self.contentText;
        [self.conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(18);
            make.left.mas_equalTo(12);
            make.right.mas_equalTo(-12);
            make.bottom.mas_equalTo(self.line1View.mas_top).offset(-10);
        }];

    }else {
        
        [self.alertView addSubview:self.titLabel];
        self.titLabel.text = self.titleText;
        [self.titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.left.mas_equalTo(12);
            make.right.mas_equalTo(-12);
            make.height.mas_equalTo(15);
        }];
        
        [self.alertView addSubview:self.conLabel];
        self.conLabel.text = self.contentText;
        [self.conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titLabel.mas_bottom).offset(10);
            make.left.mas_equalTo(12);
            make.right.mas_equalTo(-12);
            make.bottom.mas_equalTo(self.line1View.mas_top).offset(-10);
        }];
    }
}

- (UILabel *)titLabel {
    if (!_titLabel) {
        _titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#303133" text:@"" alignment:1];
    }
    return _titLabel;
}

- (UILabel *)conLabel
{
    if (!_conLabel) {
        _conLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#494245" text:@"" alignment:1];
        _conLabel.numberOfLines = 0;
    }
    return _conLabel;
}

- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]init];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_leftButton addTarget:self action:@selector(buttomleftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightButton addTarget:self action:@selector(buttomRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

#pragma mark - 分割线
- (UIView *)line1View {
    if (!_line1View) {
        _line1View = [UIView ha_createView];
        _line1View.backgroundColor  = [UIColor colorWithHexString:@"#C0C4CC"];
    }
    return _line1View;
}
- (UIView *)line2View {
    if (!_line2View) {
        _line2View = [UIView ha_createView];
        _line2View.backgroundColor  = [UIColor colorWithHexString:@"#C0C4CC"];
    }
    return _line2View;
}

@end
